<?php

namespace Gnway\Ticket\Rest\Util;

use Gnway\Ticket\Rest\Annotation\Parser\ControllerParser;
use Gnway\Ticket\Rest\Annotation\Parser\RequestMappingParser;
use Gnway\Ticket\Rest\Annotation\Parser\ResourceParser;
use ReflectionException;

final class AnnotationScannerUtils
{

  /**
   * Don't let anyone instantiate this class
   */
  private function __construct() {}

  /**
   * Scan Controllers
   *
   * @param array  $packages
   * @param string $extension
   * @return array
   * @throws ReflectionException
   */
  public static function scanController(array $packages, string $extension = 'php'): array
  {
    $controllers = (new ControllerParser($packages, $extension))->parse();
    if ( ! $controllers) {
      return [];
    }

    return $controllers;
  }

  /**
   * Scan Request Mappings
   *
   * @param array $controllers
   * @return array
   * @throws ReflectionException
   */
  public static function scanRequestMapping(array $controllers): array
  {
    if ( ! $controllers) {
      return [];
    }
    $requestMappings = (new RequestMappingParser($controllers))->parse();
    if ( ! $requestMappings) {
      return [];
    }

    return $requestMappings;
  }

  /**
   * Scan controller properties
   *
   * @param array $controllers
   * @return array
   * @throws ReflectionException
   */
  public static function scanPropertiesResource(array $controllers): array
  {
    if ( ! $controllers) {
      return [];
    }

    $resourceMappings = (new ResourceParser($controllers))->parse();
    if ( ! $resourceMappings) {
      return [];
    }

    return $resourceMappings;
  }
}